<?php

namespace app\common\behavior;

use app\common\lib\user_operate\ModuleOperate;

/**
 * 跨域请求  行为  绑定在app/tags.php  中
 */
class PluginInstallHook
{
    public $pluginModuleId;

    protected static $param; //参数

    protected static $operateList = [ //操作列表 , 顺序排列
        "installDataTable", //step 1
        "install", //step 2
    ];

    public function run(&$params)
    {
        self::$param = $params;

        $status = self::$param['info']['status'];

        foreach (self::$operateList as $step) {

            if ($status != 4) {
                //开发中的应用跳过检测
                $step             = end(self::$operateList);
                $params['return'] = $this->$step();
                break;
            }
            //按步骤执行
            $operateResult = $this->$step();

            if ($operateResult !== true) {

                $params['return'] = $operateResult;

                break;
            }
        }
    }

    //安装数据表
    private function installDataTable()
    {
        $dbTable = new \app\admin\model\PluginDbTableModel();

        $res = $dbTable->installDataTable(self::$param['info']['name']);

        if ($res != true) {

            return $res['message'];
        }

        return true;
    }

    //执行安装
    private function install()
    {
        //模块执行安装
        $MO = new ModuleOperate();

        $MO->storeId = self::$param['info']['store_id'];

        $MO->pluginName = self::$param['info']['name'];

        $res = $MO->install();

        if ($res['status'] != 'success') {

            return $res['message'];
        }

        return true;
    }

}
